<template>
	<el-form size="medium" ref="fundForm" :model="fundForm" class="custom_select_wrapper">
		<el-table :data="fundForm.funds" size="medium" class="index_table_wrapper">
			<el-table-column label="经费类型" min-width="180">
				<template slot-scope="{ row, $index }">
					<el-form-item :prop="`funds.${$index}.type`" :rules="fundRules.type">
						<el-select v-model="row.type" placeholder="选择经费类型" @change="handleSelectChange">
							<el-option v-for="(item, index) in dictEnums['FUND_TYPE']" :key="index" :label="item.msg" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</template>
			</el-table-column>
			<el-table-column label="标题" min-width="180">
				<template slot-scope="{ row, $index }">
					<el-form-item :prop="`funds.${$index}.name`" :rules="fundRules.name">
						<el-input v-model="row.name" placeholder="输入标题"></el-input>
					</el-form-item>
				</template>
			</el-table-column>
			<el-table-column label="经费号" min-width="180">
				<template slot-scope="{ row, $index }">
					<el-form-item :prop="`funds.${$index}.fno`" :rules="fundRules.fno">
						<el-input v-model="row.fno" placeholder="输入经费号"></el-input>
					</el-form-item>
				</template>
			</el-table-column>
			<el-table-column label="总额度" min-width="180">
				<template slot-scope="{ row, $index }">
					<el-form-item :prop="`funds.${$index}.quota`" :rules="fundRules.quota">
						<sw-money-input v-model="row.quota" placeholder="输入总额度"></sw-money-input>
					</el-form-item>
				</template>
			</el-table-column>
			<el-table-column width="48">
				<template slot-scope="{ row, $index }">
					<el-button type="text" size="small" icon="sw-icon-delete" @click="handleDeleteUser($index)"></el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-button type="primary" size="small" class="add" @click="handleAddedUser()">新增经费</el-button>
	</el-form>
</template>

<script>
import { mapGetters } from 'vuex';
import emitter from 'element-ui/src/mixins/emitter';
export default {
	mixins: [emitter],
	props: {
		value: { type: Array, default: () => [] }
	},
	data() {
		return {
			fundForm: {
				funds: []
			},
			fundRules: {
				type: [{ required: true, message: '请选择经费类型', trigger: 'change' }]
			},
			tableData: []
		};
	},
	computed: {
		...mapGetters(['dictEnums'])
	},
	watch: {
		value(arr) {
			this.$set(this.fundForm, 'funds', arr);
		},
		'fundForm.funds'(arr) {
			this.$emit('input', arr);
			this.dispatch('ElFormItem', 'el.form.change', [arr]);
		}
	},
	methods: {
		handleAddedUser() {
			this.fundForm.funds.push({ type: '', name: '', fno: '', quota: '' });
		},

		handleDeleteUser(index) {
			this.fundForm.funds.splice(index, 1);
		},

		//验证表单
		validChildForm() {
			return new Promise((resolve) => {
				this.$refs['fundForm'].validate((valid) => {
					resolve(valid);
				});
			});
		},

		handleSelectChange(val) {
			if (val === '0') {
				this.$delete(this.fundRules, 'name');
				this.$delete(this.fundRules, 'fno');
				this.$delete(this.fundRules, 'quota');
			} else {
				this.$set(this.fundRules, 'name', [{ required: true, message: '请输入标题', trigger: 'blur' }]);
				this.$set(this.fundRules, 'fno', [{ required: true, message: '请输入经费号', trigger: 'blur' }]);
				this.$set(this.fundRules, 'quota', [{ required: true, message: '请输入总额度', trigger: 'blur' }]);
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.custom_select_wrapper {
	&:deep(.el-button.add) {
		margin-top: 10px;
	}
}
</style>
